package com.suixinsanren.design.state.demo.demo03;

import java.util.Random;

public class HasQuarterState implements State {

    private final GumballMachine gumballMachine;
    private final Random random = new Random(System.currentTimeMillis());

    public HasQuarterState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }

    @Override
    public void insertQuarter() {
        gumballMachine.setCurrentState(gumballMachine.getHasQuarterState());
        System.out.println("已经投入硬币，无法再次投入硬币");
    }

    @Override
    public void ejectQuarter() {
        System.out.println("硬币已退还");
        gumballMachine.setCurrentState(gumballMachine.getNoQuarterState());
    }

    @Override
    public void turnCrank() {
        System.out.println("你转动了曲柄");
        int winner = random.nextInt(10);
        if (winner == 0 && gumballMachine.getCount() > 1) {
            gumballMachine.setCurrentState(gumballMachine.getWinnerState());
        } else {
            gumballMachine.setCurrentState(gumballMachine.getSoldOutState());
        }
    }

    @Override
    public void dispense() {
        System.out.println("抱歉，已经没有糖果");
    }

    @Override
    public void refill() {

    }

    @Override
    public String toString() {
        return "等待转动曲柄";
    }
}
